package com.dragee.modules.work;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dragee.modules.work.entity.CompetitionEntity;
import com.dragee.modules.work.service.CompetitionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @author tys on 2023/5/11
 */
@Component
@EnableScheduling
public class Task {
    @Autowired
    private CompetitionService competitionService;
    @Scheduled(cron ="*/59 * * * * ?")
    public void startBaoMing() {
        // P.blue("【扫描比赛的报名时间，设置报名中】开始");
        List<CompetitionEntity> competition = competitionService.list(new QueryWrapper<CompetitionEntity>()
                .between("joinStartTime", "joinEndTime", new Date())
                .eq("status", "未开始"));
        for (CompetitionEntity competitionEntity : competition) {
            competitionEntity.setStatus("报名中");
            competitionService.saveOrUpdate(competitionEntity);
        }

        List<CompetitionEntity> competition2 = competitionService.list(new QueryWrapper<CompetitionEntity>()
                .lt( "joinEndTime", new Date())
                .eq("status", "报名中"));
        for (CompetitionEntity competitionEntity : competition2) {
            competitionEntity.setStatus("报名结束");
            competitionService.saveOrUpdate(competitionEntity);
        }
        // P.blue("【扫描比赛的报名时间，设置报名中】结束");
    }
}
